using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SaveManager : singleTon<SaveManager>
{
    string scenceName="scenceName";

    public string ScenceName
    {
        get { return PlayerPrefs.GetString(scenceName); }
    }

    protected override void Awake()
    {
        base.Awake();
        DontDestroyOnLoad(this);
    }

    private void Update()
    {
        // if (Input.GetKeyDown(KeyCode.Escape))
        // {
        //     ScenceController.Instance.TransitionToMain();
        // }
        // if (Input.GetKeyDown(KeyCode.S))
        // {
        //     SavePlayerData();
        // }
        // if (Input.GetKeyDown(KeyCode.L))
        // {
        //     LoadPlayerData();
        //     LoadPlayerPosition();
        // }
    }

    public void SavePlayerData()
    {
        Save(GameManager.Instance.playerStats.characterData, GameManager.Instance.playerStats.characterData.name);
        Vector3 SavePosition = GameManager.Instance.playerStats.transform.position;
        Save(SavePosition, "position");
        Debug.Log("保存背包数据");
        InventoryManager.Instance.SaveData();
    }

    public void LoadPlayerData()
    {
        Load(GameManager.Instance.playerStats.characterData, GameManager.Instance.playerStats.characterData.name);
        Debug.Log("读取背包数据");
        InventoryManager.Instance.LoadData();
    }
    public void LoadPlayerPosition()
    {
        Vector3 LoadPosition = JsonUtility.FromJson<Vector3>(PlayerPrefs.GetString("position"));
        GameManager.Instance.playerStats.transform.position = LoadPosition;
    }
    public void Save(object data , string key)
    {
        var jsonData = JsonUtility.ToJson(data);
        PlayerPrefs.SetString(key, jsonData);
        PlayerPrefs.SetString(scenceName, SceneManager.GetActiveScene().name);
        PlayerPrefs.Save();
    }

    public void Load(object data , string key)
    {
        if (PlayerPrefs.HasKey(key))
        {
            JsonUtility.FromJsonOverwrite(PlayerPrefs.GetString(key), data);
        }
    }

}
